home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5503 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  85 lines

  1. Path: pegasus.montclair.edu!harmon
  2. From: harmon@pegasus.montclair.edu (Derek Harmon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question on argv
  5. Date: 1 Feb 1996 00:55:15 -0500
  6. Organization: Montclair State University
  7. Message-ID: <harmon.823153381@pegasus.montclair.edu>
  8. References: <4eoq3c$45j@usenet.ucs.indiana.edu>
  9. NNTP-Posting-Host: pegasus.montclair.edu
  10. X-Newsreader: NN version 6.5.0 #68 (NOV)
  11.  
  12. ** Quoting a message from <gompa@nickel.ucs.indiana.edu> dated <31-Jan-1996>:
  13.  
  14. > I am writing a program in which I want to
  15. > retrieve the arguments argv[1] ... argv[4].
  16. > argv[1] .. argv[3] are strings of characters.
  17. > I want to use argv[4] as an integer.  Can
  18. > I do that?
  19.  
  20.     Strictly speaking, no.  argv[4] is a pointer to characters.  If you
  21. say argv[4] = 36, you are telling argv[4] to point to address 36.  On
  22. many computers, this will go BOOM.  On all others it will go ZOWIE.  :)
  23.  
  24.     If you mean use argv[4] as a pointer to an integer, it is "messy"
  25. but in the sense C allows you the freedom to shoot yourself in the foot,
  26. it can be done.
  27.  
  28. >                 how?
  29.  
  30.     Ok, here's a contrived example.  Let's assume you have a program that
  31. takes 4 command-line arguments.  The first will always be a string, the
  32. second will be an integer (no error-checking needed, really idealistic
  33. example here :) ).  Note that the "integer" is going to be received as
  34. a string.  This example will use atoi() to convert it to an integer, and
  35. then shoe-horn that integer back into argv[4].  It will then demonstrate
  36. how (for whatever reason) you can then use argv[4] as a pointer to an int.
  37.  
  38. /* admittedly, these define macros work on 32-bit (2-byte) ints, and this */
  39. /* example presumes chars are 1-byte, which are ANSI no-no assumptions.   */
  40. #define hiBYTE(x) (x & 65280)
  41. #define loBYTE(x) (x & 255)
  42.  
  43. /* comment out if your machine is little endian (PC, etc) */
  44. #define BIG_ENDIAN 1
  45.  
  46. /* main routine */
  47. int main(int argc, char *argv[])
  48. {
  49.    int i;
  50.  
  51.    if (argc < 4) return( 0 );  /* didn't get the 4 arguments! */
  52.  
  53.    /* first, print out the three character string arguments */
  54.    for (i=1; i < 4; i++) printf("argv[%d] = '%s'\n", i, argv[i]);
  55.  
  56.    i = atoi(argv[4]);  /* convert integer in string to integer value, i */
  57.  
  58.    /* note, at smallest, argv[4] is 2-bytes, "A" is 'A' + '\0', or 65 00 */
  59.    /* which is 65 in little endian, and a big number in big endian.      */
  60.  
  61. #ifdef BIG_ENDIAN
  62.    argv[0] = hiBYTE(i);
  63.    argv[1] = loBYTE(i);
  64. #else
  65.    argv[0] = loBYTE(i);
  66.    argv[1] = hiBYTE(i);
  67. #endif
  68.  
  69.    /* when printing, typecast argv[4] as a (int *).  any reference to it   */
  70.    /* as a string hereafter will be erroneous and possibly (if < 10) fatal */
  71.  
  72.    printf("argv[4] = %d\n", (int *)argv[4]);
  73.  
  74.    return( 0 );
  75. }
  76.  
  77.     The only drawback to this is, if you are willing to go through all
  78. this trouble, why not just give it its' own int variable??  :D
  79.  
  80.                                         -- Stone
  81. --
  82. ... Adopt-A-PET
  83.  
  84.  
  85.